package com.dstz.bpm.service;

import com.dstz.base.api.exception.BusinessException;
import com.dstz.base.core.util.StringUtil;
import com.dstz.base.core.util.ThreadMapUtil;
import com.dstz.bpm.act.img.BpmProcessDiagramGenerator;
import com.dstz.bpm.api.constant.ActionType;
import com.dstz.bpm.api.exception.BpmStatusCode;
import com.dstz.bpm.api.service.BpmImageService;
import com.dstz.bpm.core.manager.BpmInstanceManager;
import com.dstz.bpm.core.manager.BpmTaskStackManager;
import com.dstz.bpm.core.model.BpmInstance;
import com.dstz.bpm.core.model.BpmTaskStack;
import java.awt.Color;
import java.awt.Paint;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.RepositoryService;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DefaultInstHistImgService implements BpmImageService {
   @Autowired
   private RepositoryService repositoryService;
   @Autowired
   private ProcessEngineConfiguration processEngineConfiguration;
   @Autowired
   private BpmInstanceManager f;
   @Autowired
   private BpmTaskStackManager k;
   private Color ch = new Color(26, 179, 148);
   private Color ci = new Color(28, 132, 198);
   private Color cj = new Color(237, 85, 101);

   public InputStream draw(String actDefId, String actInstId) throws Exception {
      if (StringUtil.isEmpty(actDefId)) {
         throw new BusinessException("流程定义actDefId不能缺失", BpmStatusCode.PARAM_ILLEGAL);
      } else if (StringUtil.isEmpty(actInstId)) {
         return this.j(actDefId);
      } else {
         Map<String, Paint> nodeMap = new HashMap();
         Map<String, Paint> flowMap = new HashMap();
         Map<String, Paint> gateMap = new HashMap();
         BpmInstance instance = this.f.getByActInstId(actInstId);
         List<BpmTaskStack> stacks = this.k.getByInstanceId(instance.getId());
         Map<String, BpmTaskStack> stackMap = new HashMap();
         stacks.forEach((statck) -> {
            stackMap.put(statck.getId(), statck);
         });
         Iterator var9 = stacks.iterator();

         while(var9.hasNext()) {
            BpmTaskStack stack = (BpmTaskStack)var9.next();
            if ("sequenceFlow".equals(stack.getNodeType())) {
               String actionName = stack.getActionName();
               if (actionName.equals(ActionType.CREATE.getKey())) {
                  actionName = ActionType.START.getKey();
               }

               flowMap.put(stack.getNodeId(), this.i(actionName));
            } else if ("exclusiveGateway".equals(stack.getNodeType())) {
               gateMap.put(stack.getNodeId(), this.i(stack.getActionName()));
            } else {
               nodeMap.put(stack.getNodeId(), this.i(stack.getActionName()));
            }
         }

         ThreadMapUtil.put("DefaultInstHistImgService_nodeMap", nodeMap);
         ThreadMapUtil.put("DefaultInstHistImgService_flowMap", flowMap);
         ThreadMapUtil.put("DefaultInstHistImgService_gateMap", gateMap);
         InputStream imageStream = null;

         try {
            BpmnModel bpmnModel = this.repositoryService.getBpmnModel(actDefId);
            BpmProcessDiagramGenerator diagramGenerator = new BpmProcessDiagramGenerator();
            imageStream = diagramGenerator.generateDiagram(bpmnModel, "png", nodeMap, flowMap);
         } finally {
            IOUtils.closeQuietly(imageStream);
         }

         return imageStream;
      }
   }

   private Paint i(String action) {
      if (StringUtil.isEmpty(action)) {
         return Color.GREEN;
      } else {
         ActionType status = ActionType.fromKey(action);
         if (status == null) {
            return Color.GREEN;
         } else {
            switch(status) {
            case START:
               return this.ch;
            case AGREE:
               return this.ch;
            case SIGNAGREE:
               return this.ch;
            case OPPOSE:
               return Color.PINK;
            case SIGNOPPOSE:
               return Color.PINK;
            case REJECT:
               return Color.PINK;
            case REJECT2START:
               return Color.PINK;
            case RECOVER:
               return Color.PINK;
            case DISPENDSE:
               return Color.BLUE;
            case MANUALEND:
               return Color.DARK_GRAY;
            case WITHDRAW:
               return Color.DARK_GRAY;
            case CREATE:
               return this.cj;
            case END:
               return this.ch;
            case TASKCANCELLED:
               return this.ci;
            default:
               return Color.RED;
            }
         }
      }
   }

   private InputStream j(String actDefId) {
      BpmnModel bpmnModel = this.repositoryService.getBpmnModel(actDefId);
      return this.processEngineConfiguration.getProcessDiagramGenerator().generateDiagram(bpmnModel, "png", this.processEngineConfiguration.getActivityFontName(), this.processEngineConfiguration.getLabelFontName(), this.processEngineConfiguration.getAnnotationFontName(), this.processEngineConfiguration.getClassLoader());
   }
}
